<?php
/**
 * Created by JetBrains PhpStorm.
 * User: cky
 * Date: 3/17/11
 * Time: 11:55 AM
 * To change this template use File | Settings | File Templates.
 */

namespace News {

    class Model_Post {

        protected static $table = 'posts';

        //protected $belongs_to = array('category');

        public static function get_latest($limit = NULL, $offset = NULL)
        {
            $query = \DB::select()->from(self::$table)->order_by('published', 'DESC');

            if (is_int($limit))
            {
                $query->limit($limit);
            }

            if (is_int($offset))
            {
                $query->offset($offset);
            }

            return $query;
        }

        public static function get_by_id($id)
        {
            return \DB::select()->from(self::$table)->where('id', '=', $id)->limit(1);
        }

        public static function count()
        {
            return \DB::select(array('COUNT("id")', 'total_posts'))->from(self::$table)->execute()->get('total_posts', 0);
        }

    }

}
